package com.ruoyi.web.controller.tool;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.IpAddressUtils;
import com.ruoyi.consumer.utils.pay.HttpStream;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author: lizhouwei
 * @ date : 2023/11/21
 * @description:
 */
@Component
public class QRTool {

    @Autowired
    private RedisCache redisCache;

    public String YqmGenerate(String userId) {
        String Path = "/home/ruoyi/uploadPath/upload/yqm/" + userId + ".png";
        String params = "{\"scene\":\"yqmId=" + userId + "\"}";
        byte[] data = HttpStream.post("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + redisCache.getCacheObject("token:" + "findConsumerAccessToken"), params);
        File imageFile = new File(Path);
        //创建输出流
        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream(imageFile);
            //写入数据
            outStream.write(data);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭输出流
                outStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return IpAddressUtils.getAddressInfo() + "/profile/upload/yqm/" + userId + ".png";
    }

    //生成加注机sn二维码
    public String SnGenerate(String sn, String path, String facilitId) {
        String Path = "/home/ruoyi/uploadPath/upload/sn/" + sn + ".png";
        String params = "{\"scene\":\"" + path + "Sn=" + facilitId + "\"}";
        byte[] data = HttpStream.post("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + redisCache.getCacheObject("token:" + "findConsumerAccessToken"), params);
        File imageFile = new File(Path);
        //创建输出流
        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream(imageFile);
            //写入数据
            outStream.write(data);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭输出流
                outStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return IpAddressUtils.getAddressInfo() + "/profile/upload/sn/" + sn + ".png";
    }
}
